package com.javasm.bootdemo.common.interceptor;


import com.javasm.bootdemo.common.exception.JavasmException;
import com.javasm.bootdemo.common.exception.JavasmExceptionEnum;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptors implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //被访问的方法，执行之前，执行当前的方法
        //登录的人可以访问接口。不登录的人不能访问
        //通过session判断是否登录
        HttpSession session = request.getSession();
        //从session中获取员工信息
        Object user = session.getAttribute("user");
        if (user == null) {
            throw new JavasmException(JavasmExceptionEnum.UserNotExist);
        }

        return true;

    }
}
